#include<iostream>
#include<cstdio>
#define N 10000001
#define mod 998244353
using namespace std;
int read()
{
	char c=0;
	int sum=0;
	while (c<'0'||c>'9') c=getchar();
	while ('0'<=c&&c<='9') sum=sum*10+c-'0',c=getchar();
	return sum;
}
int fast_pow(int a,int b)
{
	int res=1,mul=a;
	while (b)
	{
		if (b&1) res=1ll*res*mul%mod;
		mul=1ll*mul*mul%mod,b>>=1;
	}
	return res;
}
struct frac
{
	int x,y;
	bool operator < (const frac &t)const
	{
		return 1ll*x*t.y<1ll*y*t.x;
	}
};
frac ans;
int T,n,lg[N+1],a[N+1],s[N+1],cnt[N+1],tong[N+1],length,dque[N+1],top,dque2[N+1],top2,A,B,C,M;
bool check(int x,int y,int z)
{
	return 1ll*(y-x)*(s[z]-s[x])<=1ll*(z-x)*(s[y]-s[x]);
}
bool checks(int x,int y,int z)
{
	return 1ll*(y-x)*(s[z]-2-s[x])>1ll*(z-x)*(s[y]-s[x]);
}
int main()
{
	freopen("guess.in","r",stdin);
	freopen("guess.ans","w",stdout);
	for (int i=2;i<=N;++i) lg[i]=lg[i>>1]+1;
	T=read();
	while (T--)
	{
		n=read(),a[0]=read(),A=read(),B=read(),C=read(),M=read(),length=top=top2=0,ans=(frac){1,1};
		for (int i=1;i<=n;++i) cnt[i]=0;
		for (int i=1;i<=n;++i) a[i]=(1ll*A*a[i-1]%M*a[i-1]%M+1ll*B*a[i-1]%M+C)%M+1,cnt[a[i]]++;
		for (int i=1;i<=n;++i)
			if (cnt[i])
				tong[++length]=min(cnt[i],2)+1;
		for (int i=1;i<=length;++i) s[i]=s[i-1]+tong[i];
		for (int i=2;i<=length;++i)
			for (int j=0;j<=i-2;++j)
				ans=min(ans,(frac){i-j,s[i]-s[j]-2});
		printf("%0.5lf %lld\n",(double)(ans.x)/ans.y,1ll*ans.x*fast_pow(ans.y,mod-2)%mod);
	}
	return 0;
}
